package cn.edu.thu.tsquality.core.common.table;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by wangyihan  on 2018/4/2 下午5:04. E-mail address is yihanwang22@163.com. Copyright © 2017
 * wangyihan. All Rights Reserved.
 *
 * @author wangyihan
 */
public class Table implements Serializable {

  private final Header header;
  private List<Row> rows;
  private int pos;

  public Table(Header header, List<Row> rows) {
    this.header = header;
    this.rows = rows;
    this.pos = rows.size();
  }

  public Table(Map<String, Integer> attrMap, List<List<Object>> rows) {
    this.header = new Header(attrMap);
    this.rows = new ArrayList<>(rows.size());
    for (List<Object> row : rows) {
      this.rows.add(new Row(this.header, row, pos));
      pos++;
    }
  }

  public void addRow(List<Object> rowData) {
    this.rows.add(new Row(this.header, rowData, pos));
    pos++;
  }

  public void addRow(Row r) {
    this.rows.add(r);
    pos++;
  }

  public Row get(int index) {
    return this.rows.get(index);
  }

  public Header getHeader() {
    return this.header;
  }

  public List<Row> getRows() {
    return this.rows;
  }

  public int size() {
    return this.rows.size();
  }

  public Table clone() {
    List<Row> cloneRows = new ArrayList<>();
    for (int i = 0; i < rows.size(); i++) {
      cloneRows.add(rows.get(i).clone());
    }
    Table cloneTable = new Table(header, cloneRows);
    return cloneTable;
  }

  public int getPos() {
    return this.pos;
  }

  public void setPos(int pos) {
    this.pos = pos;
  }

  public Row getFirstRow() {
    if (rows == null || rows.size() == 0) {
      return null;
    }
    return rows.get(0);
  }
}
